﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;
using System.Web.Security;
using System.Web.Profile;

namespace EHTSaleManager.UICustomer
{
    public partial class CustomerOrderFinish : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet dsRegion = RegionProvider.GetRegions();
                this.ddlReceivedRegion.DataValueField = "RegionID";
                this.ddlReceivedRegion.DataTextField = "RegionName";
                this.ddlReceivedRegion.DataSource = dsRegion;
                this.ddlReceivedRegion.DataBind();
            }
        }

        private Boolean ValidateData()
        {
            string message = "";
            Boolean isSuccess = true;
            
            if (ddlReceivedRegion.SelectedValue =="-1")
            {

                message = "Select a region";
                isSuccess = false;
            }
            DateTime x;
            DateTime y = new DateTime();
            x = cdRequredDate.SelectedDate;
            if (x == y)
            {
                rDate.IsValid = false;
                isSuccess = false;
            }
            if (x.Date < DateTime.Now.Date) {
                rDate.ErrorMessage = "Required date must be equal or greater than current date.";
                rDate.IsValid = false;
                isSuccess = false;
            }
            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }

        protected void ddlReceivedRegion_DataBound(object sender, EventArgs e)
        {
            this.ddlReceivedRegion.Items.Insert(0, new ListItem("select region ...", "-1"));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
                return;

            Page.Validate();

            if (Page.IsValid)
            {
                MembershipUser theUser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                Order theOrder = new Order();
                theOrder.BillPictureUrl = "";
                theOrder.CustomerID = theUser.ProviderUserKey.ToString();
                theOrder.OrderID = -1;
                theOrder.OrderState = HangSo.ORDER_STATE_APPENDING;
                theOrder.ReceicedPhone = this.txtReceicedPhone.Text.Trim();
                theOrder.ReceivedAddress = this.txtReceivedAddress.Text.Trim();
                theOrder.ReceivedCity = this.txtReceivedCity.Text.Trim();
                theOrder.ReceivedName = this.txtReceivedName.Text.Trim();
                theOrder.ReceivedPostalCode = this.txtPostCode.Text.Trim();
                theOrder.ReceivedRegionID = Int32.Parse(this.ddlReceivedRegion.SelectedValue.ToString());
                theOrder.RequireDate = cdRequredDate.SelectedDate.AddHours(DateTime.Now.Hour + 5);
                theOrder.TotalAmount = ShoppingCart.Instance.GetSubTotal();
                theOrder.ServiceCost = RegionProvider.GetRegionByRegionID(Int32.Parse(this.ddlReceivedRegion.SelectedValue.ToString())).ServiceCost;
                // cập nhật vào DB
                ShoppingCart.Instance.theOrder = theOrder;
                ShoppingCart.Instance.SaveDataBase();
                HttpContext.Current.Session["ASPNETShoppingCart"] = null; ; // xóa thông tin ShoppingCart

                // cập nhật vào tài khoản Customer phí dịch vụ
                ProfileBase theProfile = ProfileBase.Create(HttpContext.Current.User.Identity.Name);
                float serviceCostOfRegion = RegionProvider.GetRegionByRegionID(theOrder.ReceivedRegionID).ServiceCost; // phí dịch vụ theo miền
                //float toltalAccountCurrent = float.Parse(theProfile.GetPropertyValue("ToltalAccount").ToString()); // tổng tài khoản hiện tại
                //float toltalAccountNow = serviceCostOfRegion + toltalAccountCurrent; // tổng tài khoảng thay thế
                
                // update vao CurrentBalance or LastBalance
                //Start
                int LevelServiceID = Convert.ToInt32(theProfile.GetPropertyValue("ServiceLevelID"));
                AccountBalanceProvider.UpdateAccountBalanceByCustomerID(theUser.ProviderUserKey.ToString(), serviceCostOfRegion, LevelServiceID);
                //End
                //theProfile.SetPropertyValue("ToltalAccount", toltalAccountNow);
                theProfile.Save();
                Response.Redirect("CustomerOrderFinished.aspx");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("CustomerViewCart.aspx");
        }
    }
}